package com.promise.jokerdream.response.pc

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.decodeFromJsonElement

// 武林盟主信息
@Serializable
data class QueryWlmzResponse(
    override val result: Int,
    override val msg: String = "",
    @SerialName("is_sign_up")
    val isSignUp: Int = 0,
    // 当前报名类型，0为未报名、1为黄金、2为白银、3为青铜
    @SerialName("ground_id")
    val groundId: Int = 0,
    // 当前状态，1为领奖期、3为报名期、5为猜排名期、6为八强决斗
    val status: Int = 0,
    @SerialName("award_info")
    val awardInfo: JsonElement? = null,
    // 猜排名信息
    @SerialName("guess_info")
    val guessInfo: WlmzGuessModel = WlmzGuessModel()
): BasePCResponse {
    override fun getDescription(): String {
        return msg
    }
    
    /**
     * 解析 awardInfo 为 WlmzAwardModel 列表
     */
    fun getAwardInfoList(): List<WlmzAwardModel> {
        val json = Json { ignoreUnknownKeys = true }
        return try {
            when (awardInfo) {
                is JsonArray -> {
                    awardInfo.map { element ->
                        json.decodeFromJsonElement<WlmzAwardModel>(element)
                    }
                }
                is JsonObject -> {
                    listOf(json.decodeFromJsonElement<WlmzAwardModel>(awardInfo))
                }
                else -> emptyList()
            }
        } catch (e: Exception) {
            // 解析失败时返回空列表
            emptyList()
        }
    }
}

@Serializable
data class WlmzAwardModel(
    // 当前赛季数
    @SerialName("section_id")
    val sectionId: Int = 0,
    // 当前轮数
    @SerialName("round_id")
    val roundId: Int = 0,
)

@Serializable
data class WlmzGuessModel(
    @SerialName("ground_tips")
    val groundTips: String = "",
    @SerialName("guess_confirm")
    val guessConfirm: Int = 0,
)
